package com.surelution.lansao

import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.NameValuePair
import org.apache.http.client.HttpClient
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
import org.apache.http.message.BasicNameValuePair
import org.apache.http.util.EntityUtils

import com.surelution.lansao.vo.HttpClientContainer


class PartnerInfoController {
	private Partner partner
	
	def beforeInterceptor = {
		if(params.flashId) {
			def x = X.findByUuid(params.flashId)
			if(x) {
				partner = Partner.get(x.who)
			}
		}
	}
	

    def index = {
		render(view:"create", params:[partner:partner, flashId:params.flashId])
	}

	def save = {
		def username = params.username
		partner.name = params.name
		partner.mobileNo = params.mobileNo
		partner.tel = params.tel
		if(username) {
//			def user = partner.user
			//TODO just to test
			def user = User.findByUsername(username)
			if(!user) {
				user = User.findByUsername(username)
				if(!user) {
					user = new User(username:username, password:"111111", enabled:true)
					if(user.save(flush:true)) {
						partner.user = user
						partner.active = false
						partner.save(flush:true)
						render(view: "verify", params:[flashId:params.flashId, partner:partner])
						return
					} else {
						flash.message = "系统出了个错误，请稍等试试看"
					}
				} else {
				flash.message = "这个用户名已经被注册了，请重新换一个试试看"
			}
			}
			
		} else {
			flash.message = "用户明不能为空"
		}
		render(view: "create", params: [flashId:params.flashId])
	}

	def verifyImg = {
		def container = servletContext.getAttribute(HttpClientContainer.KEY)
        HttpClient httpclient = container.findClient(params.flashId)
		HttpGet httpget = new HttpGet("http://mp.weixin.qq.com/cgi-bin/verifycode?r=0.885284654097316");
		HttpResponse respImg = httpclient.execute(httpget);
		HttpEntity entityImg = respImg.getEntity();
		
		entityImg.writeTo(response.getOutputStream());
		EntityUtils.consume(entityImg);
		response.flushBuffer()
	}
	
	def verify = {
		println params.flashId
		def container = servletContext.getAttribute(HttpClientContainer.KEY)
		HttpClient httpclient = container.findClient(params.flashId)
		def verifycode = params.verifyCode
        HttpPost httpost = new HttpPost("http://mp.weixin.qq.com/cgi-bin/emailregister?lang=zh_CN");
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("email", "${partner.user.username}@weixindingcan.com"));
        nvps.add(new BasicNameValuePair("verifycode", verifycode));
        nvps.add(new BasicNameValuePair("pwd", "96e79218965eb72c92a549dd5a330112"));
        nvps.add(new BasicNameValuePair("ajax", "1"));
        httpost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();
		
        EntityUtils.consume(entity);
		render("谢谢您的耐心，还需最后一步，您就完成了注册。大约3分钟内，您刚输入的手机将会收到一条短信，请直接短信，请将短信中的6位验证码通过我们的微信账号回复给我们，即完成整个过程")
	}
}
